/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.ArrayList;

/**
 *
 * @author Afshin
 */
public class Agent extends Thread {

    Double locx, locy;
    int agentid;
    ArrayList<Integer> friends, enemies;
    ArrayList<String> likes, dislikes,playerlikes,playerdislikes;
    ArrayList<Event> myEvents;
    ArrayList<Boolean> answers;
    FSMachine fsm;
    boolean isLiving, isKiller, isBusy, deathclock;
    int deathCounter;
    String agentname;
    //boolean isBusy;

    public Agent(int agentId) {
        isBusy = false;
        setAgentid(agentId);
        fsm = new FSMachine();
        fsm.Create(agentId);
        deathCounter = -1;
        playerlikes = new ArrayList<String>();
        playerdislikes = new ArrayList<String>();
        answers = new ArrayList<Boolean>();
    }

    public int getAgentid() {
        return agentid;
    }

    public void setAgentid(int agentid) {
        this.agentid = agentid;
    }

    public String getAgentname() {
        return agentname;
    }

    public void setAgentname(String agentname) {
        this.agentname = agentname;
    }

    public boolean isLiving() {
        return isLiving;
    }

    public void setIsLiving(boolean isLiving) {
        this.isLiving = isLiving;
    }

    public boolean isKiller() {
        return isKiller;
    }

    public void setIsKiller(boolean isKiller) {
        this.isKiller = isKiller;
    }

    public ArrayList<String> getDislikes() {
        return dislikes;
    }

    public void setDislikes(ArrayList<String> dislikes) {
        this.dislikes = dislikes;
    }

    public boolean isBusy() {
        return isBusy;
    }

    public void setBusy(boolean isBusy) {
        this.isBusy = isBusy;
    }

    public int getDeathCounter() {
        return deathCounter;
    }

    public void setDeathCounter(int deathCounter) {
        this.deathCounter = deathCounter;
    }

    public void setLiving(boolean isLiving) {
        this.isLiving = isLiving;
    }

    public void setKiller(boolean isKiller) {
        this.isKiller = isKiller;
    }

    public ArrayList<Integer> getEnemies() {
        return enemies;
    }

    public void setEnemies(ArrayList<Integer> enemies) {
        this.enemies = enemies;
    }

    public ArrayList<Integer> getFriends() {
        return friends;
    }

    public void setFriends(ArrayList<Integer> friends) {
        this.friends = friends;
    }

    public FSMachine getFsm() {
        return fsm;
    }

    public void setFsm(FSMachine fsm) {
        this.fsm = fsm;
    }

    public ArrayList<String> getLikes() {
        return likes;
    }

    public void setLikes(ArrayList<String> likes) {
        this.likes = likes;
    }

    public Double getLocx() {
        return locx;
    }

    public void setLocx(Double locx) {
        this.locx = locx;
    }

    public Double getLocy() {
        return locy;
    }

    public void setLocy(Double locy) {
        this.locy = locy;
    }

    public ArrayList<Event> getMyEvents() {
        return myEvents;
    }

    public void setMyEvents(ArrayList<Event> myEvents) {
        this.myEvents = myEvents;
    }

    public void run() {
    }

    public void step() {
    	if (isLiving) {
    		fsm.Update();
    		
    		// death logic 
    		if (deathCounter > 0) {
    			deathCounter--;
    			if (deathCounter == 0) {
    				isLiving = false;
    				Global.killInProgress = false;
                    for(int i=0; i<friends.size(); i++){
                        Global.agents.get(friends.get(i)).myEvents.add(new Event(locx,locy,friends.get(i),agentid,"Killing"));
                        //System.out.println("I lived with dignity and now I left my legacy to my friends");
                    }
                        System.out.println("I DIED LOL!!!!!");
    				fsm.AddEvent(FSMachine.enumEvent.KILLING);
    				fsm.Update();
    			}
    		}
    	}
    }

    public void tellFriendsEvent(String type, int witness, int victim) {
        for (int i = 0; i < friends.size(); i++) {
            Global.agents.get(friends.get(i)).myEvents.add(new Event(locx, locy, witness, victim, type));
            //System.out.println("I lived with dignity and now I left my legacy to my friends");
        }
    }

    public ArrayList<Double> updateDistances() {
        ArrayList<Double> distances = new ArrayList<Double>();
        for (int i = 0; i < friends.size(); i++) {
            double dist = distanceTo(friends.get(i));
            distances.add(dist);
        }
        return distances;
    }

    public ArrayList<Double> updateDistancesKiller() {
        ArrayList<Double> distances = new ArrayList<Double>();
        for (int i = 0; i < enemies.size(); i++) {
            double dist = distanceTo(enemies.get(i));
            distances.add(dist);
        }
        return distances;
    }

    public void getInfo() {
        System.out.println("Name:" + agentname);
        System.out.println("Location:" + locx + "," + locy);
        for (int i = 0; i < friends.size(); i++) {
            System.out.println("Distance to my friend " + Global.agents.get(friends.get(i)).getAgentname() + "=" + distanceTo(friends.get(i)));
        }
    }

    public double distanceTo(int id) {
        double dist = 0.0;
        dist = Math.pow(locx - Global.agents.get(id).getLocx(), 2.0) + Math.pow(locy - Global.agents.get(id).getLocy(), 2.0);
        dist = Math.sqrt(dist);
        return dist;
    }

    public int getMinIndex(ArrayList<Double> dists) {
        int index = 0;
        double dist = dists.get(0);
        return index;
    }
    
    @Override
    public String toString() {
    	return "["
    			+ "killer:" + isKiller + "\n"
    			+ "busy:" + isBusy + "\n"
    			+ "living:" + isLiving + "\n"
    			+ "]";
    }
}
